/*
题目链接:https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/?envType=daily-question&envId=2025-03-23
*/

//题解代码:
class Solution {
public:
    bool canBeValid(string s, string locked) {
        int n = s.size();
        if(n&1) return false;
        int mi = 0,mx = 0;
        for(int i=0;i<n;++i){
            if(locked[i]=='0'){
                mx += 1;
                mi -= 1;
                if(mi<0) mi = 1;
            }else{
                if(s[i] == '('){
                    mx += 1;
                    mi += 1;
                }else{
                    mx -= 1;
                    mi -= 1;
                    if(mx<0) return false;
                    if(mi<0) mi = 1;
                }
            }
        }
        return mi == 0;
    }
};
